﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.Product;
using PetaPoco;

namespace BrnMall.Manager
{
    public static class ProductAppointManager
    {
        #region 商品预约

        public static int AddAppoint(ProductAppoint appoint)
        {
            using (var db = new QDMallDatabase())
            {
                
                var id= (int)db.Insert(appoint);
                db.Execute("update bma_products set AppointCount=AppointCount+1 where pid=@0", appoint.Pid);
                return id;
            }
        }

        public static bool UpdateAppointStatus(int aid,int status)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute("update bma_productappoint set status="+status+"  where appointid=@0", aid) > 0;
            }
        }

        public static bool DeleteAppoint(int aid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute("update bma_productappoint set deleted=1  where appointid=@0", aid) > 0;
            }
        }

        public static List<ProductAppoint> GetProductAppointList(int uid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<ProductAppoint>("select * from bma_productappoint where uid=@0 and deleted=0 order by createdtime desc", uid);
            }
        }

        public static Page<ProductAppoint> List(int uid, int pid, string startTime, string endTime,int page,int itemsPerPage,int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                var sql=new Sql();
                sql.Select("*");
                sql.From("bma_productappoint");
                var where = " 1=1";
                if (uid > 0)
                    where += " and uid=" + uid;
                if (pid > 0)
                    where += " and pid=" + pid;
                if (storeId > 0)
                    where += " and storeid=" + storeId;
                if (!string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime))
                    where += " and DATEDIFF(day,ApplyTime,'" + startTime + "')>=0 and  DATEDIFF(day,ApplyTime,'" +
                             endTime + "')<=0";
                where += " and deleted=0";
                sql.Where(where);
                sql.OrderBy("createdtime desc");
               return db.Page<ProductAppoint>(page, itemsPerPage, sql);
            }
        }

        public static ProductAppoint Get(int aid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<ProductAppoint>(aid);
            }
        }
        public static bool Check(int uid,DateTime applyTime)
        {
            using (var db = new QDMallDatabase())
            {
                var r= db.Fetch<ProductAppoint>("select * from bma_productappoint where uid=@0 and ApplyTime=@1 and deleted=0",uid,applyTime);
                return r.Count > 0;
            }
        }
        #endregion
    }
}
